package zhujie.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {

    @Pointcut("execution(* zhujie.service.impl.QueryServiceImpl.*(..))")
    public void query() {};
    @Pointcut("execution(* zhujie.service.impl.ModifyServiceImpl.*(..))")
    public void modify() {};

    @Before("query()")
    public void before(JoinPoint joinPoint){
        System.out.println(
                "target:\t" + joinPoint.getTarget() +
                "\nargs:\t" + joinPoint.getArgs() +
                "\nmethod name:\t" + joinPoint.getSignature().getName() +
                "\nbefore.......");
    }
    @AfterReturning(value="query()",returning ="ret")
    public void after(JoinPoint joinPoint,Object ret) {
        System.out.println(
                "target:\t" + joinPoint.getTarget() +
                "\nargs:\t" + joinPoint.getArgs() +
                "\nmethod name:\t" + joinPoint.getSignature().getName() +
                "\nreturn value:\t" + ret +
                "\nafter.......");
    }
    @AfterThrowing(value = "modify()",throwing = "exception")
    public void afterThrows(JoinPoint joinPoint,Exception exception) {
        System.out.println("Exception found:\n " + exception.getMessage());
    }

    @Around(value = "modify()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("Around before....");
        Object ret = joinPoint.proceed();
        System.out.println("Around after.....");
        return ret;
    }

}
